package com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Dept;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.FirstCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.service.ICustomerDetailsService;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.entity.RootImageInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.entity.SearchRootImageInfo;
import com.sxfq.loansupermarket.backsystem.modular.qualityAssurance.service.IDentalImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 质量管理控制器
 *
 * @author fengshuonan
 * @Date 2019-09-01 15:34:05
 */
@Controller
@RequestMapping("/rootOrthodontics")
public class RootOrthodonticsImageController extends BaseController {
    @Autowired
    IDentalImageService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    ICustomerDetailsService customerDetailsService;

    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/doctorLooked/";

    /**
     * 正畸影像列表
     */
    @RequestMapping(value = "/list/{reservationsId}")
    @ResponseBody
    public Object list(SearchRootImageInfo info, @PathVariable String reservationsId, HttpServletRequest request) {
        Page<RootImageInfo> page = new PageFactory<RootImageInfo>().defaultPage();
        try {
            info.setReservationsId(reservationsId);
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<RootImageInfo> list = service.selectPageListByOrthodontics(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.selectPageListByOrthodontics(page, info);
            }
            page.setTotal(service.getOrthodonticsPageCount(page, info));
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 质量检测正畸影像列表
     */
    @RequestMapping(value = "/rootList")
    @ResponseBody
    public Object rootList(SearchRootImageInfo info, HttpServletRequest request) {
        Page<RootImageInfo> page = new PageFactory<RootImageInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<RootImageInfo> list = service.selectPageListByOrthodontics1(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = service.selectPageListByOrthodontics1(page, info);
            }
            page.setTotal(service.getOrthodonticsPageCount1(page, info));
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    @RequestMapping(value = "/orthodontics_seeDiagnosis/{rea}/{tabId}")
    public String doctor_seeDiagnosis(@PathVariable String rea ,@PathVariable int tabId, Model model) throws Exception {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String reservationsId = rea.split(",")[0];
        String arrivalStore = rea.split(",")[1];
        String selabb = "select id ,abbreviation from dept where simplename ='"+arrivalStore+"'";
        Dept info = sqlMapper.selectOne(selabb,Dept.class);
        String curCus = "select id from "+info.getAbbreviation()+"customer_info where reservations_id ='"+reservationsId+"'  and arrival_time is not null order by arrival_time DESC limit 1 ";
        CustomerInfo curInfo = sqlMapper.selectOne(curCus, CustomerInfo.class);
        String sql = "select " +
                " (CASE t2.customer_sex  WHEN 1 THEN '男' WHEN 2 THEN '女' ELSE '未知' END ) as sex ," +
                " (CASE t2.marital_status WHEN 1 THEN '已婚' WHEN 2 THEN '未婚' WHEN 3 THEN '离异' ELSE '未知' END ) as maritalStatusName ," +
                "t2.* from " + info.getAbbreviation() + "customer_info t2 where id= '" + curInfo.getId() + "';";
        CustomerInfo customerInfo = sqlMapper.selectOne(sql, CustomerInfo.class);
        FirstCustomerInfo firstCustomerInfo = customerDetailsService.firstVisitInformation(customerInfo.getReservationsId()); //首次到店的患者信息
        if (firstCustomerInfo == null) {
            return "/doNoClose.html";
        }
        customerDetailsService.getCustomerInformationsList();
        customerInfo.setOrthodonticNumber(customerDetailsService.getorthodonticNumber(customerInfo.getReservationsId()));
        customerInfo.setFirstAppointmentPayService(firstCustomerInfo.getAppointmentPayService());
        customerInfo.setFirstArchiving(firstCustomerInfo.getArchiving());
        customerInfo.setFirstArrivalTime(firstCustomerInfo.getArrivalTime());
        customerInfo.setFirstFieldTc(firstCustomerInfo.getFieldTc());
        customerDetailsService.intodayOperation(customerInfo);
        model.addAttribute("occupationList", ShiroKit.getUser().getOccupationInfoList());
        model.addAttribute("refereeShipList", ShiroKit.getUser().getRefereeShipInfoList());
        model.addAttribute("tabId", tabId);
        model.addAttribute("customerInfo", customerInfo);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        model.addAttribute("auditswitch",redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Auditswitch",0));
        model.addAttribute("roleNameLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"RoleName",0));
        model.addAttribute("dealyesLogin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Dealyes",0));
        model.addAttribute("isRefundAdmin", redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"IsRefundAdmin",0));
        model.addAttribute("storeValue", ShiroKit.getUser().getOutpatientName());
        return PREFIX + "doctor_Details.html";
    }

    //获取正畸 图片
    @RequestMapping(value = "/getImgcancel", method = RequestMethod.POST)
    @ResponseBody
    public List<RootImageInfo> getImgcancel(String num, String iconType, String classification) throws Exception {
        String sql = "select * from root_orthodontics_imgs_info where num='" + num + "' and imgtpye='" + iconType + "' and classification='" + classification + "'";
        List<RootImageInfo> infoList = sqlMapper.selectList(sql, RootImageInfo.class);
        return infoList;
    }

}
